home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-06-16 | 14.2 KB | 568 lines | [TEXT/MPS ] |
- /*
- File: Common.c
-
- Copyright: © 1997-1998 by Apple Computer, Inc., all rights reserved.
-
- */
-
- //
- // You may incorporate this sample code into your applications
- // without restriction. This sample code has been provided "AS
- // IS" and the responsibility for its operation is 100% yours.
- // You are not permitted to redistribute the source as "Apple
- // sample code" after having made changes. If you're going to
- // re-distribute the source, we require that you make it clear
- // in the source that the code was descended from Apple sample
- // code, but that you've made changes.
- //
-
- #pragma segment AppSeg
-
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
-
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
-
- #ifndef __ICONS__
- #include <Icons.h>
- #endif
-
- #ifndef __GESTALT__
- #include <Gestalt.h>
- #endif
-
- #ifndef Common_Defs
- #include "Common.h"
- #endif
-
- extern short gInBackground;
-
- // routines used for getting file path names:
- OSErr PPrepend(StringPtr dst,StringPtr src);
- void PAppend(StringPtr dst, StringPtr src);
-
- TrapType GetTrapType(short theTrap);
- short NumToolboxTraps(void);
-
-
- // ********************************************************************************
- // * ConcatPP()
- // ********************************************************************************
- unsigned char* ConcatPP(unsigned char* a, unsigned char* b)
- {
- short i;
- for (i = 1;i <= b[0];i++)
- a[++a[0]] = b[i];
- return (a);
- }
-
-
- // ********************************************************************************
- // * FSSpecsEq()
- // ********************************************************************************
- Boolean FSSpecsEq(FSSpec* a,FSSpec* b)
- {
- if (a->vRefNum != b->vRefNum)
- return (false);
- if (a->parID != b->parID)
- return (false);
- if (IUEqualString(a->name,b->name))
- return (false);
- return (true);
- }
-
-
- // ********************************************************************************
- // * rnd()
- // * Returns a 16-bit random number no large than 'max'.
- // ********************************************************************************
- static SInt16 rnd(SInt16 max)
- {
- return ((UInt32) max * (UInt16) Random()) >> 16;
- }
-
-
- // ********************************************************************************
- // * RandTween()
- // * Returns an 8-bit number between low and high.
- // ********************************************************************************
- short RandTween(short low, short high)
- {
- short result;
-
- result = Random() % (high + 1 - low);
- if (result < 0)
- result = -result;
- result += low;
- return(result);
- }
-
-
- // ********************************************************************************
- // * GetItemStr()
- // ********************************************************************************
- unsigned char* GetItemStr(DialogPtr theDialog, short theItem, unsigned char* theString)
- {
- Handle ahandle;
- Rect arect;
- short atype;
-
- GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
- GetDialogItemText(ahandle,theString);
- return (theString);
- }
-
-
- // ********************************************************************************
- // * PokeItemStr()
- // ********************************************************************************
- void PokeItemStr(DialogPtr theDialog, short theItem, unsigned char *theString)
- {
- Handle ahandle;
- Rect arect;
- short atype;
-
- GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
- SetDialogItemText(ahandle,theString);
- }
-
-
- // ********************************************************************************
- // * PokeCtlVal()
- // ********************************************************************************
- void PokeCtlVal(DialogPtr theDialog, short theItem, short value)
- {
- Handle ahandle;
- Rect arect;
- short atype;
-
- GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
- SetControlValue((ControlHandle)ahandle,value);
- }
-
-
- // ********************************************************************************
- // * PokeCtlHilite()
- // ********************************************************************************
- void PokeCtlHilite(DialogPtr theDialog, short theItem, short value)
- {
- Handle ahandle;
- Rect arect;
- short atype;
-
- GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
- HiliteControl((ControlHandle)ahandle,value);
- }
-
-
- // ********************************************************************************
- // * MyP2CCopy()
- // ********************************************************************************
- char* MyP2CCopy(unsigned char* psrc, char* ctarget)
- {
- short i;
- for(i = 0;i < psrc[0];i++)
- ctarget[i] = psrc[i + 1];
- ctarget[i] = 0;
- return (ctarget);
- }
-
-
- // ********************************************************************************
- // * MyC2PStr()
- // ********************************************************************************
- unsigned char* MyC2PStr(char* theStr)
- {
- short i,length;
-
- for(i = 0;theStr[i];i++)
- ;
- length = i;
- while(i)
- {
- theStr[i] = theStr[i - 1];
- i--;
- }
- ((unsigned char*)theStr)[0] = length;
- return ((unsigned char *)theStr);
- }
-
-
- // ********************************************************************************
- // * MyP2CStr()
- // ********************************************************************************
- char* MyP2CStr(unsigned char* theStr)
- {
- short i,length;
- length = theStr[0];
- for(i = 0;i < length;i++)
- theStr[i] = theStr[i + 1];
- theStr[length] = 0;
- return ((char *)theStr);
- }
-
-
- // ********************************************************************************
- // * DebugCStr()
- // ********************************************************************************
- void DebugCStr(char* msg)
- {
- MyC2PStr(msg);
- DebugStr((unsigned char*)msg);
- MyP2CStr((unsigned char*)msg);
- }
-
-
- // ********************************************************************************
- // * Str2OSType()
- // * C strings only.
- // ********************************************************************************
- OSType Str2OSType(Str255 theStr)
- {
- OSType num = 0;
- num = theStr[3] + (theStr[2] << 8) + (theStr[1] << 16) + (theStr[0] << 24);
- return num;
- }
-
- // ********************************************************************************
- // * MyStrLen()
- // * C strings only.
- // ********************************************************************************
- long MyStrLen(char* s)
- {
- long count=0;
- while (*s++ != '\0') count++;
- return count;
- }
-
-
- // ********************************************************************************
- // * myStringToLong()
- // * C strings only.
- // ********************************************************************************
- long myStringToLong(char* s)
- {
- long n = 0;
- short i;
-
- for (i=0;s[i]>='0' && s[i]<='9';i++)
- n = 10*n + (s[i]-'0');
- return n;
- }
-
-
- // ********************************************************************************
- // * myStringToShort()
- // * C strings only.
- // ********************************************************************************
- short myStringToShort(char* s)
- {
- short n = 0;
- short i;
-
- for(i=0;s[i]>='0' && s[i]<='9';i++)
- n = 10*n + (s[i]-'0');
- return n;
- }
-
-
- // ********************************************************************************
- // * myStrCpy()
- // * C strings only.
- // ********************************************************************************
- void myStrCpy(char* dst, char* src)
- {
- while(*src != '\0')
- *dst++ = *src++;
- *dst = '\0';
- }
-
-
- // ********************************************************************************
- // * myStrCat()
- // ********************************************************************************
- void myStrCat(char* dst, char* src)
- {
- dst = dst + MyStrLen(dst);
- myStrCpy(dst,src);
- }
-
-
- // ********************************************************************************
- // * flag routines for KeyDown events..
- // ********************************************************************************
- Boolean OptionDown() { return ModifierDown(optionKey); }
- Boolean ShiftDown() { return ModifierDown(shiftKey); }
- Boolean CommandDown() { return ModifierDown(cmdKey); }
- Boolean ControlDown() { return ModifierDown(controlKey); }
-
-
- // *****************************************************************************
- // *
- // * ModifierDown()
- // *
- // *****************************************************************************
- Boolean ModifierDown(short modifier)
- {
- EventRecord event;
- (void) OSEventAvail(0,&event);
- return ((event.modifiers & modifier) != 0);
- }
-
-
- // *****************************************************************************
- // *
- // * hiliteTheButton()
- // *
- // *****************************************************************************
- void hiliteTheButton(DialogPtr theDialog, short whichItem)
- {
- short itemType = 0;
- Handle itemHdl;
- Rect itemRect;
- long int startTick;
-
- GetDialogItem(theDialog,whichItem,&itemType,&itemHdl,&itemRect);
- HiliteControl((ControlHandle)itemHdl,1);
- for (startTick=TickCount();TickCount()-startTick<kDelayTick;)
- {} // do nothing...
- HiliteControl((ControlHandle)itemHdl,kActive);
- }
-
-
- // *****************************************************************************
- // *
- // * AdornButton()
- // *
- // *****************************************************************************
- void AdornButton(DialogPtr theDialog, short whichItem)
- {
- GrafPtr oldPort;
- Handle itemH;
- short itemType;
- Rect itemRect;
- long version = 0;
- OSErr theErr = noErr;
-
- theErr = Gestalt(gestaltSystemVersion,&version);
- if (version <= 0x0760) // this look is only for Systems after 7.6.0
- {
- GetPort(&oldPort);
-
- SetPort(theDialog);
- GetDialogItem(theDialog,whichItem,&itemType,&itemH,&itemRect);
- PenSize(3,3);
- InsetRect(&itemRect,-4,-4);
- FrameRoundRect(&itemRect,14,14);
-
- PenNormal();
- SetPort(oldPort);
- }
- }
-
-
- // *****************************************************************************
- // *
- // * DrawIconSuite()
- // *
- // * This routine draws the appropriate icon into 'destRect' based on the
- // * 'resID' of the icon suite. The caller must set its port before calling
- // * this routine.
- // *
- // *****************************************************************************
- void DrawIconSuite(short resID, Rect destRect)
- {
- IconSelectorValue iconType;
- IconAlignmentType align;
- IconTransformType transform;
- OSErr theErr = noErr;
- Handle iconSuiteHdl;
-
- iconType = svAllAvailableData;
- theErr = GetIconSuite(&iconSuiteHdl,resID,iconType);
- if ((theErr == noErr) && (iconSuiteHdl != nil))
- {
- align = atAbsoluteCenter;
- transform = ttNone;
- theErr = PlotIconSuite(&destRect,align,transform,iconSuiteHdl);
- }
- }
-
-
- // ********************************************************************************
- // *
- // * PAppend()
- // *
- // ********************************************************************************
- void PAppend(StringPtr dst, StringPtr src)
- {
- BlockMove(src + 1, dst + *dst + 1, *src);
- *dst += *src;
- }
-
-
-
- // ********************************************************************************
- // *
- // * PPrepend()
- // * or "Path Append": add 'src' to the end of 'dst'.
- // * - Pascal strings only.
- // *
- // ********************************************************************************
- OSErr PPrepend(StringPtr dst,StringPtr src)
- {
- if(*dst + *src > 255)
- return(-1);
-
- BlockMove(dst + 1, dst + *src + 1, *dst);
- BlockMove(src + 1, dst + 1, *src);
- *dst += *src;
-
- return(0);
- }
-
-
- // ********************************************************************************
- // *
- // * PathNameFromDirID()
- // *
- // * Creates a full or partial path name of a given file.
- // * - Inputs 'dirID' and 'vRefNum' and outputs 'fullPathName'.
- // * - fullPathLength = num characters requested.
- // * - full = flag to make full path, rather than just the item name.
- // *
- // ********************************************************************************
- OSErr PathNameFromDirID(long dirID,
- short vRefNum,
- StringPtr fullPathName,
- short maxPathLength,
- short full)
- {
- DirInfo block;
- Str255 directoryName;
- OSErr theErr;
-
- fullPathName[0] = 0;
-
- block.ioDrParID = dirID;
- block.ioNamePtr = directoryName;
- do {
- block.ioVRefNum = vRefNum;
- block.ioFDirIndex = -1;
- block.ioDrDirID = block.ioDrParID;
- theErr = PBGetCatInfoSync((CInfoPBPtr)&block);
- if (theErr)
- return(theErr);
-
- PAppend(directoryName,(StringPtr)"\p:");
-
- if (fullPathName[0] + directoryName[0] >= maxPathLength)
- return(-1); // actual path name too long for the requested path name size
-
- PPrepend(fullPathName,directoryName);
- }
- while ((block.ioDrDirID != 2) && (full));
-
- return(0);
- }
-
-
- // *****************************************************************************
- // *
- // * AdjustCursor()
- // *
- // *****************************************************************************
- void AdjustCursor(Point theLoc, RgnHandle theRgn)
- {
- WindowPtr theWindow;
- Document* theDocument;
- RgnHandle arrowRgn, iBeamRgn, hiliteRgn;
- Rect theRect;
- Point thePoint;
-
- if (gInBackground)
- return;
-
- arrowRgn = NewRgn();
- SetRectRgn(arrowRgn,-32767,-32767,32767,32767);
-
- if ((theWindow = FrontWindow()) &&
- (theDocument = IsDocumentWindow(theWindow)) &&
- (theDocument->theTE != NULL))
- {
- SetPort(theWindow);
-
- iBeamRgn = NewRgn();
- hiliteRgn = NewRgn();
-
- theRect = (**(theDocument->theTE)).viewRect;
- LocalToGlobal((Point*)&(theRect.top));
- LocalToGlobal((Point*)&(theRect.bottom));
- RectRgn(iBeamRgn,&theRect);
-
- CopyRgn(theDocument->hiliteRgn,hiliteRgn);
- thePoint.h = thePoint.v = 0;
- LocalToGlobal(&thePoint);
- OffsetRgn(hiliteRgn,thePoint.h,thePoint.v);
-
- DiffRgn(arrowRgn,hiliteRgn,arrowRgn);
- DiffRgn(arrowRgn,iBeamRgn,arrowRgn);
-
- DiffRgn(iBeamRgn,hiliteRgn,iBeamRgn);
-
- if (PtInRgn(theLoc,iBeamRgn))
- {
- SetCursor(*GetCursor(iBeamCursor));
- CopyRgn(iBeamRgn,theRgn);
- }
- else
- {
- if (PtInRgn(theLoc,hiliteRgn))
- {
- SetCursor(&qd.arrow);
- CopyRgn(hiliteRgn,theRgn);
- }
- else
- {
- SetCursor(&qd.arrow);
- CopyRgn(arrowRgn,theRgn);
- }
- }
-
- DisposeRgn(iBeamRgn);
- DisposeRgn(hiliteRgn);
- }
- else
- {
- SetCursor(&qd.arrow);
- CopyRgn(arrowRgn,theRgn);
- }
-
- DisposeRgn(arrowRgn);
- }
-
-
- OSErr AEGetDescData(const AEDesc *desc, DescType *typeCode, void *dataBuffer, ByteCount maximumSize, ByteCount *actualSize)
- {
- *typeCode = desc->descriptorType;
- Handle h = (Handle)desc->dataHandle;
- ByteCount dataSize = GetHandleSize(h);
- if (dataSize > maximumSize)
- *actualSize = maximumSize;
- else
- *actualSize = dataSize;
- BlockMoveData(*h, dataBuffer, *actualSize);
- return noErr;
- }
-
- void DoLowMemAlert( )
- {
- Str255 message;
- GetIndString( message, rAppStringsID, sLowMemoryErr );
- ParamText( (ConstStr255Param)message, "\p", "\p", "\p" );
- InitCursor( );
- (void)NoteAlert( rGenericAlertID, 0L );
- }